package com.whzm.service.Impl;

import com.whzm.mapper.CompanyUserMapper;
import com.whzm.mapper.PersonalUserMapper;
import com.whzm.mapper.UserMapper;
import com.whzm.pojo.CompanyUser;
import com.whzm.pojo.PersonalUser;
import com.whzm.pojo.User;
import com.whzm.pojo.vo.CUser;
import com.whzm.pojo.vo.PUser;
import com.whzm.service.UserService;
import com.whzm.util.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@SuppressWarnings("all")
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    CompanyUserMapper companyUserMapper;

    @Autowired
    PersonalUserMapper personalUserMapper;

    @Autowired
    RedisServiceImpl redisService;

    /**
     * 判断电话号码是否已经注册过
     * @param phone
     * @return true 存在 false不存在
     */
    @Transactional(readOnly = true)
    private boolean isExistPhone(String phone){
        User user = userMapper.searchInfoByPhone(phone);
        if(user != null){
            return  true;
        }
        return false;
    }

    /**
     * 判断redis中验证码是否匹配
     * @param phone
     * @param phoneCode
     * @return
     */
    private boolean isTruePhoneCode(String phone,String phoneCode){
        //从redis中获取验证码
        String code = (String)redisService.get("p" + phone);

        if(code!=null && code.equals(phoneCode)){
            return true;
        }
        return false;
    }

    /**
     *
     * 将cUser进行拆分，拆分成User和CompanyUser,并存入数据库中
     * @param cUser
     * @return
     */
    private boolean addCUser(CUser cUser){
        if(cUser != null){
            //user中的信息
            String uId = "U"+String.valueOf(System.currentTimeMillis() + "" + (int) (Math.random() * 100)).substring(3);
            int type = 2,isDeleted = 0;
            String status = "0";
            User user = new User();
            user.setId(uId);
            user.setUsername(cUser.getUsername());
            user.setPhone(cUser.getPhone());
            user.setPassword(cUser.getPassword());
            user.setType(type);
            user.setStatus(status);
            user.setWechat(cUser.getWechat());
            user.setIsDeleted(isDeleted);

            //company中的信息
            String cId = "U"+String.valueOf(System.currentTimeMillis() + "" + (int) (Math.random() * 100)).substring(3);
            CompanyUser companyUser = new CompanyUser(cId,cUser.getName(),cUser.getPosition(),uId,0);

            int flag1 = userMapper.addUser(user);
            int flag2 = companyUserMapper.addCompanyUser(companyUser);

            if(flag1 == 1 || flag2 == 1)
                return true;
        }
        return false;
    }

    /**
     * 将pUser进行拆分，拆分成User和PersonalUser,并存入数据库中
     * @param pUser
     * @return
     */
    private boolean addPUser(PUser pUser){
        if(pUser != null){
            //user中的信息
            String uId = "R"+String.valueOf(System.currentTimeMillis() + "" + (int) (Math.random() * 100)).substring(3);
            int type = 2,isDeleted = 0;
            String status = "0";
            User user = new User();
            user.setId(uId);
            user.setUsername(pUser.getUsername());
            user.setPhone(pUser.getPhone());
            user.setPassword(pUser.getPassword());
            user.setType(type);
            user.setStatus(status);
            user.setWechat(pUser.getWechat());
            user.setIsDeleted(isDeleted);

            //person中的信息
            String pId = "R"+String.valueOf(System.currentTimeMillis() + "" + (int) (Math.random() * 100)).substring(3);
            PersonalUser personalUser = new PersonalUser(pId,pUser.getIdNumber(),uId,0);

            int flag1 = userMapper.addUser(user);
            int flag2 = personalUserMapper.addPersonUser(personalUser);

            if(flag1 == 1 || flag2 == 1)
                return true;
        }
        return false;
    }

    /**
     * 公司用户注册添加用户信息
     * @param cUser 前端传递数据放在中间类cUser
     * @return
     */
    @Override
    public ResponseEntity addCompanyUser(CUser cUser) {
        //先判断该电话号码是否已经注册
        boolean isExistPhone = isExistPhone(cUser.getPhone());
        //判断验证码是否正确(验证码过期或者验证码不正确)
        boolean isTruePhoneCode = isTruePhoneCode(cUser.getPhone(),cUser.getPhoneCode());
        boolean isSuccess = false;

        //如果电话未注册，并且验证码也正确，则将cUser进行拆分，然后存入数据库中
        if(!isExistPhone && isTruePhoneCode){
            isSuccess = addCUser(cUser);
            if(isSuccess){
                //注册成功在redis中删除验证码信息
                redisService.remove("p" + cUser.getPhone());
                //返回响应体消息
                return ResponseEntity.tipsSuccess(200,"注册成功");
            }
        }

        if(isExistPhone){
            return ResponseEntity.tipsSuccess(407,"电话号码已注册");
        }

        if(!isTruePhoneCode){
            return ResponseEntity.tipsSuccess(408,"验证码不正确");
        }

        return ResponseEntity.tipsSuccess(405,"其他错误");
    }

    /**
     * 个人用户注册添加用户信息
     * @param pUser  前端传递数据放在中间类pUser
     * @return
     */
    @Override
    public ResponseEntity addPersonalUser(PUser pUser) {
        //先判断该电话号码是否已经注册
        boolean isExistPhone = isExistPhone(pUser.getPhone());
        //判断验证码是否正确(验证码过期或者验证码不正确)
        boolean isTruePhoneCode = isTruePhoneCode(pUser.getPhone(),pUser.getPhoneCode());
        boolean isSuccess = false;

        //如果电话未注册，并且验证码也正确，则将cUser进行拆分，然后存入数据库中
        if(!isExistPhone && isTruePhoneCode){
            isSuccess = addPUser(pUser);
            if(isSuccess){
                //注册成功在redis中删除验证码信息
                redisService.remove("p" + pUser.getPhone());
                //返回响应体消息
                return ResponseEntity.tipsSuccess(200,"注册成功");
            }
        }

        if(isExistPhone){
            return ResponseEntity.tipsSuccess(407,"电话号码已注册");
        }

        if(!isTruePhoneCode){
            return ResponseEntity.tipsSuccess(408,"验证码不正确");
        }

        return ResponseEntity.tipsSuccess(405,"其他错误");
    }

}
